package com.platform.server.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 日期工具类
 */
public class DateTools {

    /**
     * 变量：日期格式化类型 - 格式:yyyy-MM-dd
     */
    public static final int DEFAULT = 0;
    public static final int YM = 1;
    /**
     * 变量：日期格式化类型 - 格式:yyyy-MM-dd
     */
    public static final int YMR_SLASH = 11;
    /**
     * 变量：日期格式化类型 - 格式:yyyyMMdd
     */
    public static final int NO_SLASH = 2;
    /**
     * 变量：日期格式化类型 - 格式:yyyyMM
     */
    public static final int YM_NO_SLASH = 3;
    /**
     * 变量：日期格式化类型 - 格式:yyyy-MM-dd HH:mm:ss
     */
    public static final int DATE_TIME = 4;
    /**
     * 变量：日期格式化类型 - 格式:yyyyMMddHHmmss
     */
    public static final int DATE_TIME_NO_SLASH = 5;
    /**
     * 变量：日期格式化类型 - 格式:yyyy-MM-dd HH:mm
     */
    public static final int DATE_HM = 6;
    /**
     * 变量：日期格式化类型 - 格式:HH:mm:ss
     */
    public static final int TIME = 7;
    /**
     * 变量：日期格式化类型 - 格式:HH:mm
     */
    public static final int HM = 8;
    /**
     * 变量：日期格式化类型 - 格式:HHmmss
     */
    public static final int LONG_TIME = 9;
    /**
     * 变量：日期格式化类型 - 格式:HHmm
     */
    public static final int SHORT_TIME = 10;
    /**
     * 变量：日期格式化类型 - 格式:yyyy-MM-dd HH:mm:ss
     */
    public static final int DATE_TIME_LINE = 12;
    /**
     * yyMMddHHmmssSSS
     */
    public static final String YY_MM_DD_HH_MM_SS_SSS = "yyMMddHHmmssSSS";
    private static Logger log = LoggerFactory.getLogger(DateTools.class);

    public static String dateToStr(Date date, int type) {
        switch (type) {
            case DEFAULT:
                return dateToStr(date);
            case YM:
                return dateToStr(date, "yyyy-MM");
            case NO_SLASH:
                return dateToStr(date, "yyyyMMdd");
            case YMR_SLASH:
                return dateToStr(date, "yyyy-MM-dd");
            case YM_NO_SLASH:
                return dateToStr(date, "yyyyMM");
            case DATE_TIME:
                return dateToStr(date, "yyyy-MM-dd HH:mm:ss");
            case DATE_TIME_NO_SLASH:
                return dateToStr(date, "yyyyMMddHHmmss");
            case DATE_HM:
                return dateToStr(date, "yyyy-MM-dd HH:mm");
            case TIME:
                return dateToStr(date, "HH:mm:ss");
            case HM:
                return dateToStr(date, "HH:mm");
            case LONG_TIME:
                return dateToStr(date, "HHmmss");
            case SHORT_TIME:
                return dateToStr(date, "HHmm");
            case DATE_TIME_LINE:
                return dateToStr(date, "yyyy-MM-dd HH:mm:ss");
            default:
                throw new IllegalArgumentException("Type undefined : " + type);
        }
    }

    public static String dateToStr(Date date, String pattern) {
        if (date == null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    public static String dateToStr(Date date) {
        return dateToStr(date, "yyyy-MM-dd");
    }

    /**
     * 得到几天前/后的时间。
     *
     * @param d
     * @param day 正数为几天后，负数为几天前。
     * @return
     */
    public static Date getDateBeforeOrAfter(Date d, int day) {
        if (null == d) {
            return null;
        }
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(d);
            now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
            return now.getTime();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 得到几分钟前/后的时间。
     *
     * @param d
     * @param minutes 正数为几分钟后，负数为几分钟前。
     * @return
     */
    public static Date getDateBeforeOrAfterMinutes(Date d, int minutes) {
        if (null == d) {
            return null;
        }
        try {
            Calendar now = Calendar.getInstance();
            now.setTime(d);
            now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + minutes);
            return now.getTime();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获得几天前/后的时间 字符串
     *
     * @param d
     * @param n
     * @param pattern
     * @return
     */
    public static String getDateBeforeOrAfterSTr(Date d, int n, String pattern) {
        if (null == d || StringTools.isNull(pattern)) {
            return null;
        }
        Date targetDate = getDateBeforeOrAfter(d, n);
        return DateTools.date2Str(targetDate, pattern);
    }

    /**
     * 获得对于今天的几天前/后的时间 字符串
     *
     * @param n
     * @param pattern
     * @return
     */
    public static String getDateFromNow(int n, String pattern) {
        if (StringTools.isNull(pattern)) {
            return null;
        }
        Date targetDate = getDateBeforeOrAfter(new Date(), n);
        return DateTools.date2Str(targetDate, pattern);
    }

    /**
     * 日期转换为字符串
     *
     * @param date
     * @return
     */
    public static String date2Str(Date date, String pattern) {
        if (null == date || StringTools.isNull(pattern)) {
            return null;
        }
        String str = null;
        try {
            SimpleDateFormat smf = new SimpleDateFormat(pattern);
            str = smf.format(date);
        } catch (Exception e) {
        }
        return str;
    }

    /**
     * 验证是否是某种格式的日期字符串
     *
     * @param str
     * @param pattern
     * @return
     */
    public static boolean isDate(String str, String pattern) {
        if (StringTools.isNull(str) || StringTools.isNull(pattern)) {
            return false;
        }
        boolean isDate = false;
        try {
            SimpleDateFormat smf = new SimpleDateFormat(pattern);
            Date date = smf.parse(str);
            if (null != date) {
                isDate = true;
            }
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return isDate;
    }

    /**
     * 获取当前日期的格式化时间字符串
     *
     * @return
     */
    public static String getCurrentTimeStr(String pattern) {
        if (StringTools.isNull(pattern)) {
            return "";
        }
        String dateStr = "";
        try {
            SimpleDateFormat smf = new SimpleDateFormat(pattern);
            dateStr = smf.format(new Date());
        } catch (Exception e) {
        }
        return dateStr;
    }

    /**
     * 字符串转换为日期
     */
    public static Date str2Date(String dateStr, String pattern) {
        if (StringTools.isNull(dateStr) || StringTools.isNull(pattern)) {
            return null;
        }
        SimpleDateFormat smf = new SimpleDateFormat(pattern);
        try {
            return smf.parse(dateStr);
        } catch (Exception e) {
        }
        return null;
    }

    public static Date dateCal(String dateStr, String pattern, int type, int scope) {

        if (StringTools.isNull(dateStr) || StringTools.isNull(pattern)) {
            return null;
        }
        try {
            SimpleDateFormat smf = new SimpleDateFormat(pattern);
            Date d = smf.parse(dateStr);
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            c.add(type, scope);
            return c.getTime();
        } catch (Exception e) {
        }
        return null;
    }

    public static Date dateCompute(Date date, int type, int scope) {

        if (null == date) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(type, scope);
        return c.getTime();
    }

    public static int calDayBetween(Date from, Date to) {

        long data = to.getTime() - from.getTime();
        System.out.println((int) (data / 1000 / 60 / 60 / 24) + 1);
        return (int) (data / 1000 / 60 / 60 / 24) + 1;
    }

    public static int calHourBetween(Date from, Date to) {

        long data = to.getTime() - from.getTime();
        return (int) (data / 1000 / 60 / 60) + 1;
    }

    public static int calSecondBetween(Date from, Date to) {

        long data = to.getTime() - from.getTime();
        return (int) (data / 1000) + 1;
    }

    public static Date simple2(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }
}
